package com.cmccgz.udacs_landlord_webapp.jpush;

import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cicadamob.contact.api.Utils.StringUtil;
import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.AccreditDetailBean;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.orhanobut.logger.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 访客授权界面
 * Created by June on 2017-8-15.
 */
public class AccreditActivity extends BaseActivity implements AccreditContract.View, View.OnClickListener {

    private static final String TAG = AccreditActivity.class.getSimpleName();
    private ViewHelper mViewHelper;
    private AccreditContract.Presenter mPresenter;
    private EditText et_username, et_usermobile, et_requesttime, et_deviceName, et_reason, et_device, et_stime, et_etime, et_times;
    private Button btn_refuse, btn_agree;
    private int times = -1;
    private String deviceId;
    private String requestId;
    private String startTime;
    private String endTime;
    List<AccreditDetailBean.DevicesBean> deviceList;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_accredit;
    }

    @Override
    protected void initView() {
        requestId = getIntent().getStringExtra("requestId");
        Logger.t(TAG).d("requestId============" + requestId);
        mPresenter = new AccreditPresenter(this);
        et_username = (EditText) findViewById(R.id.et_username);
        et_usermobile = (EditText) findViewById(R.id.et_usermobile);
        et_requesttime = (EditText) findViewById(R.id.et_requesttime);
        et_deviceName = (EditText) findViewById(R.id.et_deviceName);
        et_reason = (EditText) findViewById(R.id.et_reason);
        et_device = (EditText) findViewById(R.id.et_device);
        et_stime = (EditText) findViewById(R.id.et_stime);
        et_etime = (EditText) findViewById(R.id.et_etime);
        et_times = (EditText) findViewById(R.id.et_times);
        btn_refuse = (Button) findViewById(R.id.btn_refuse);
        btn_agree = (Button) findViewById(R.id.btn_agree);
        et_username.setKeyListener(null);
        et_usermobile.setKeyListener(null);
        et_requesttime.setKeyListener(null);
        et_deviceName.setKeyListener(null);
        et_reason.setKeyListener(null);
        et_device.setKeyListener(null);
        et_stime.setKeyListener(null);
        et_etime.setKeyListener(null);
        et_times.setKeyListener(null);
        et_device.setOnClickListener(this);
        et_stime.setOnClickListener(this);
        et_etime.setOnClickListener(this);
        et_times.setOnClickListener(this);
        btn_refuse.setOnClickListener(this);
        btn_agree.setOnClickListener(this);
        mPresenter.start();
        mPresenter.getAccreditDetail(AppConfig.getMobile(this), requestId);
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {
        mViewHelper = ViewHelper.instance(mContext);
        if (mViewHelper != null) {
            mViewHelper.setTitle("访客审核");
            mViewHelper.setAllVisable(false);
            mViewHelper.setImageRightVisable(true);
            mViewHelper.setImageRight(R.mipmap.icon_close);
        }
    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    public void showAccreditInfo(String name, String mobile, String requestTime, String deviceName, String reason, List<AccreditDetailBean.DevicesBean> deviceList) {
        et_username.setText(name);
        et_usermobile.setText(mobile);
        et_requesttime.setText(requestTime);
        et_deviceName.setText(deviceName);
        et_reason.setText(reason);
        this.deviceList = deviceList;
        if (deviceList != null && deviceList.size() > 0) {
            deviceId = deviceList.get(0).getDevice_id();
            et_device.setText(deviceList.get(0).getDevice_address());
        }
        mPresenter.selectDefaultTime();
        enableView(true);
    }

    @Override
    public void requestFail(String message) {
        ToastUtils.show(this, message);
        enableView(false);
    }

    @Override
    public void handleSuccess() {
        dismissLoadingDialog();
        showTipsDialog("提示", "处理成功", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setResult(RESULT_OK);
                AccreditActivity.this.finish();
            }
        }, new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                setResult(RESULT_OK);
                AccreditActivity.this.finish();
            }
        });
    }

    @Override
    public void handleFail() {
        ToastUtils.show(this, "处理失败");
        dismissLoadingDialog();
    }

    @Override
    public void wasHandled() {
        ToastUtils.show(this, "该请求已授权");
        enableView(false);
    }

    /**
     * 操作按钮控制
     * @param isEnable
     */
    private void enableView(boolean isEnable){
        btn_refuse.setTextColor(ContextCompat.getColor(this, isEnable ? R.color.black : R.color.gray_c));
        btn_agree.setTextColor(ContextCompat.getColor(this, isEnable ? R.color.blue_d : R.color.gray_c));
        et_device.setTextColor(ContextCompat.getColor(this, isEnable ? R.color.black : R.color.gray_c));
        et_stime.setTextColor(ContextCompat.getColor(this, isEnable ? R.color.black : R.color.gray_c));
        et_etime.setTextColor(ContextCompat.getColor(this, isEnable ? R.color.black : R.color.gray_c));
        et_times.setTextColor(ContextCompat.getColor(this, isEnable ? R.color.black : R.color.gray_c));
        btn_refuse.setEnabled(isEnable);
        btn_agree.setEnabled(isEnable);
        et_device.setEnabled(isEnable);
        et_stime.setEnabled(isEnable);
        et_etime.setEnabled(isEnable);
        et_times.setEnabled(isEnable);
    }

    @Override
    public void setStartTime(String datetime) {
        startTime = datetime;
        et_stime.setText(datetime);
    }

    @Override
    public void setEndTime(String datetime) {
        endTime = datetime;
        et_etime.setText(datetime);
    }

    @Override
    public void setTimes(int times) {
        if (times == 0)
            this.times = -1;
        else
            this.times = times;
        et_times.setText(this.times == -1 ? "无限次" : this.times + "次");
    }

    @Override
    public void setDevice(String deviceId, String deviceAddress) {
        this.deviceId = deviceId;
        et_device.setText(deviceAddress);
    }

    @Override
    public void setDefaultTime(String startTime, String endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        et_stime.setText(startTime);
        et_etime.setText(endTime);
    }

    @Override
    public void showLoadingDialog() {
        super.showLoadingDialog("正在提交内容，请稍后...");
    }

    @Override
    public void setPresenter(AccreditContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_right:
                this.finish();
                break;
            case R.id.et_device:
                if (deviceList != null && deviceList.size() > 0) {
                    String[] devicesId = new String[deviceList.size()];
                    String[] devicesAddress = new String[deviceList.size()];
                    for (int i = 0; i < deviceList.size(); i++) {
                        devicesId[i] = deviceList.get(i).getDevice_id();
                        devicesAddress[i] = deviceList.get(i).getDevice_address();
                    }
                    mPresenter.selectDevice(this, devicesId, devicesAddress);
                } else {
                    ToastUtils.show(this, "没有门禁可选");
                }
                break;
            case R.id.et_stime:
                mPresenter.selectDateTime(this, AccreditPresenter.TYPE_START);
                break;
            case R.id.et_etime:
                mPresenter.selectDateTime(this, AccreditPresenter.TYPE_END);
                break;
            case R.id.et_times:
                mPresenter.selectTimes(this);
                break;
            case R.id.btn_refuse:
                if (StringUtil.isEmpty(requestId)) {
                    ToastUtils.show(this, "REQUEST_Id IS NULL");
                    return;
                }
                mPresenter.refuseBox(this, requestId);
                break;
            case R.id.btn_agree:
                if (StringUtil.isEmpty(requestId)) {
                    ToastUtils.show(this, "请求ID为空");
                    return;
                }
                if (StringUtil.isEmpty(deviceId)) {
                    ToastUtils.show(this, "设备ID为空");
                    return;
                }
                if(!compareDate(startTime, endTime)){
                    ToastUtils.show(this, "结束时间必须大于开始时间");
                    return;
                }
                mPresenter.handleAccredit(requestId, "true", "", deviceId, this.startTime, this.endTime, this.times + "");
                break;
            default:
                break;
        }
    }

    public boolean compareDate(String startTime, String endTime) {
        try {
            SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date sDate = dft.parse(startTime);
            Date eDate = dft.parse(endTime);
            if (eDate.getTime() > sDate.getTime()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }
}
